home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvdraw / demos / dsp_mgr / dsp_mgr.c next >
Encoding:
C/C++ Source or Header  |  1997-07-10  |  15.7 KB  |  557 lines

  1. /*
  2. |    file name - dsp_mgr.c
  3. |===================================================================
  4. |
  5. |    This example is a DV-Draw display manager.  The screen is
  6. |    divided into 2 areas.  One is used for the display menu,
  7. |    the other is for displaying DV-Draw applications.  The areas
  8. |    can be defined by a DV-Draw layout file.
  9. |
  10. |    The menu area should contain objects that are "named" the
  11. |    view file to be "played" in the display area.
  12. |
  13. |    The display views can be a simple DV-Draw view or one with
  14. |    RULES.
  15. |
  16. |       The example also traverses the menu view and saves all the
  17. |    referenced demos in a list.  If the user selects an object
  18. |    named "loop", the display manager starts looping through
  19. |    its list.  Each display is active for a period of time. To
  20. |    break the loop, users can just select another display.
  21. |    The default number of views you can "loop" between is 50.
  22. |    The default display time is 15 seconds.
  23. |
  24. |    To quit the display manager, the user can "quit" the window
  25. |    or select an object named "quit" from the display menu.
  26. |
  27. |    You can overwrite the default state by using the following
  28. |    arguments:
  29. |        argv[1] - device          (default is DVDEVICE)
  30. |        argv[2] - menu            (default is dsp_menu.v)
  31. |        argv[3] - initial display (default is dsp_mgr.v)
  32. |        argv[4] - layout view     (default is dsp_mgr.lay)
  33.  
  34. |===================================================================
  35. */
  36. #ifdef WINNT
  37. #include <windows.h>
  38. #endif
  39. #include "std.h"
  40. #include "dvstd.h"
  41. #include "dvtools.h"
  42. #include "dvinteract.h"
  43. #include "dvGR.h"
  44. #include "GRfundecl.h"
  45. #include "Tfundecl.h"
  46. #include "VOfundecl.h"
  47. #include "VUfundecl.h"
  48. #include "VUerfundecl.h"
  49. #include "MISCfuns.h"
  50.  
  51. #include "dsp_mgr.h"            /* Variables and Constants */
  52. #include "dsp_timer.h"          /* Code for timing the "loop" */
  53. #ifndef WINNT
  54. /* Include the X based files so we can add AppTimeOuts */
  55. #ifdef CONST
  56. #undef CONST
  57. #endif
  58.  
  59. #ifndef __STDC__
  60. #define _NO_PROTO
  61. #endif
  62.  
  63. /*
  64.  *  X11 include files
  65.  */
  66. #include <X11/Xlib.h>
  67. #include <X11/Intrinsic.h>
  68.  
  69. LOCAL XtAppContext app_context;
  70. LOCAL unsigned int TimeoutInterval = 25;
  71.  
  72. LOCAL VOID time_out_proc V_P_((ADDRESS args, XtIntervalId *interval_id));
  73. #endif
  74. CHAR *VIstrclone V_P_((CHAR *));
  75.      /* Internal funciton: allocates and clones strings */
  76.  
  77. #define DEF_PATH    (CHAR*)NULL
  78. #define DEF_DISPFORMS    (CHAR*)NULL
  79. #define DEF_COLORTABLE    (CHAR*)NULL
  80.  
  81. #ifdef WINNT
  82. CHAR dsp_message[1024];
  83.  
  84. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  85.                      LPSTR lpCmdLine,  int nCmdShow  )
  86. #else
  87. int
  88. main (argc, argv)
  89.      INT argc;
  90.      CHAR *argv[];
  91. #endif
  92.   {
  93.   /* argv[1] - display device (default is defined by DVDEVICE) */
  94.   /* argv[2] - menu (default is defined in dsp_mgr.h) */
  95.   /* argv[3] - initial display (default is defined in dsp_mgr.h) */
  96.   /* argv[4] - layout view (default is defined in dsp_mgr.h) */
  97.  
  98.   CHAR *device = NULL;
  99.   DV_BOOL done = NO;
  100.   CHAR *menu_view_name, *display_view_name, *layout_view_name;
  101.   OBJECT location;
  102.  
  103. #ifdef WINNT
  104.   INT argc;
  105.   CHAR **argv;
  106.  
  107.   /* Initialize the arguments */
  108.   make_argv(&argc,&argv,lpCmdLine);
  109. #endif
  110.  
  111.   /* Initialize DV-Tools using the default DVconfig path */
  112.   (VOID) TInit (DEF_PATH, DEF_DISPFORMS);
  113.  
  114.   /* Initialize the screen */
  115.   device = (argc > 1) ? argv[1] : NULL;
  116.   ScreenInit (argv[0], device);
  117.  
  118.  
  119.  
  120.  
  121.   /* Initialize the Display Areas */
  122.   menu_view_name = (argc > 2) ? argv[2] : MenuViewName;
  123.   display_view_name = (argc > 3) ? argv[3] : DisplayViewName;
  124.   layout_view_name = (argc > 4) ? argv[4] : LayoutViewName;
  125.   DisplayInit (menu_view_name, display_view_name, layout_view_name);
  126.  
  127. #ifndef WINNT
  128.   /* Get the Xt based information */
  129.   GRget (V_X_APPLIC_CONTEXT, &app_context, V_END_OF_LIST);
  130.  
  131.   /* Post a timeout for dynamic updates
  132.   |  The timeout procedure will update the dynamics of
  133.   |  all screens which have been opened. The procedure is invoked
  134.   |  whenever the specified time interval elapses. The interval is
  135.   |  specified in milliseconds.
  136.   */
  137.   XtAppAddTimeOut (app_context, TimeoutInterval,
  138.            (XtTimerCallbackProc)time_out_proc, NULL);
  139. #endif
  140.   /* Go into a loop, handling user updates and updating displays */
  141.   while (!done)
  142.     {
  143.  
  144. #ifdef WINNT
  145.  
  146.     /* Update the Display */
  147.     HandleUpdates();
  148.  
  149.     /* Get the Event */
  150.     location = VOloWinEventPoll( V_NO_WAIT );
  151.  
  152. #else
  153.      /* See if there is an event to handle, only user input events
  154.      |  matching those set in VOscWinEventMask will be returned.
  155.      |
  156.      |  NOTE: VOloWinEventPoll will get the next event and then dispatch
  157.      |  non-DataViews events. Our posted AppTimeOut event to handle
  158.      |  dynamics, will be called as needeb by VOloWinEventPoll, which
  159.      |  calls XtAppNextEvent and XtDispatchEvent.
  160.      */
  161.      location =  VOloWinEventPoll( V_WAIT );
  162. #endif
  163.  
  164.       /* Handle Event */
  165.       if( location )
  166.         HandleEvents( location, &done );
  167.  
  168.     }
  169.  
  170.   /*  Cleanup */
  171.   DisplayCleanup ();
  172.   (VOID) TTerminate ();
  173.  
  174.   return EXIT_OK;
  175. }
  176. #ifndef WINNT
  177. LOCAL VOID 
  178. time_out_proc (args, interval_id)
  179.      ADDRESS args;
  180.      XtIntervalId *interval_id;
  181. {
  182.   /* Handle Updates */
  183.   HandleUpdates();
  184.  
  185.   XtAppAddTimeOut (app_context, TimeoutInterval,
  186.            (XtTimerCallbackProc)time_out_proc, NULL);
  187. }
  188. #endif
  189.  
  190. LOCAL void
  191. HandleUpdates()
  192. {
  193.   /* If we are looping, switch demos */
  194.   if( DemoLooping && TimedOut() )
  195.     SwitchDemo();
  196.  
  197.   /* If we are displaying a demo, update it */
  198.   if( Proto_Env )
  199.     TprotoUpdate( Proto_Env );
  200. }
  201.  
  202.  
  203. /*------------------------------------------------------------------------*/
  204. LOCAL void
  205. ScreenInit (program_name, device)
  206.      CHAR *program_name, *device;
  207. {
  208.     int error_code=0;
  209.     char buf[50];
  210.    /* Open the screen using DVDEVICE and the default color table.
  211.    |  Also, name the window, and offset it from the upper left corner.
  212.    */
  213.    VUoff_copyright();
  214.    DVscreen = TscOpenSet( device, DEF_COLORTABLE,
  215.                 V_WINDOW_NAME, "DataViews Feature Demo",
  216.                         V_ACTIVE_CURSOR,
  217. #ifdef WINNT
  218. #ifdef DOUBLE_BUFFER
  219.                         V_WIN32_DOUBLE_BUFFER,  YES,
  220. #endif
  221. #else  
  222.             V_X_EXPOSURE_BLOCK, YES,
  223. #endif
  224.                 V_END_OF_LIST);
  225.    
  226.  
  227.   /* If we couldn't open a device, exit the program */
  228.   if (!DVscreen) {
  229.      error_code=TscOpenError();
  230.   
  231. #ifdef WINNT
  232.      if((error_code>=6)&&(error_code<=9))
  233.      {
  234.        sprintf(buf,"Product is not validated. Error code %d.",error_code);
  235.        MessageBox(NULL,buf,"Validation Error",MB_OK);
  236.      }
  237.      else
  238.      {
  239.        (VOID)sprintf( dsp_message,"\nDataViews environment variable DVDEVICE must be defined.\n  or\nUsage: %s <device>\n", program_name);
  240.         MessageBox ( GetFocus (), dsp_message, "Display Manager", MB_ICONSTOP );
  241.      }
  242. #else
  243.       if((error_code>=6)&&(error_code<=9))
  244.         fprintf(stderr,"Product is not validated. Error code %d.",error_code);
  245.       else
  246.       {
  247.         (VOID) printf ("\nDataViews environment variable DVDEVICE must be defined.\n");
  248.         (VOID) printf ("  or\n");
  249.         (VOID) printf ("Usage: %s <device>\n", program_name);
  250.       }
  251. #endif
  252.       exit (EXIT_ERR);
  253.     }
  254.  
  255.   /* Erase the copyright message */
  256.   (VOID) TscErase (DVscreen);
  257.  
  258.   /* Define the types of user inputs to gather */
  259.   (VOID) VOscWinEventMask ((ULONG)
  260.          V_KEYPRESS | V_KEYRELEASE | V_BUTTONPRESS | V_BUTTONRELEASE |
  261.            V_MOTIONNOTIFY | V_ENTERNOTIFY | V_LEAVENOTIFY | V_EXPOSE |
  262.                            V_RESIZE | V_WINDOW_QUIT, 0L);
  263. }
  264.  
  265. /*------------------------------------------------------------------------*/
  266. LOCAL void
  267. DisplayInit (menu_name, display_name, layout_name)
  268.      CHAR *menu_name, *display_name, *layout_name;
  269. {
  270.  
  271.   VIEW view;
  272.   RECTANGLE wvp, box, dummy;
  273.   INT i;
  274.  
  275.   /* Get the display areas from the layout file */
  276.   view = TviLoad (layout_name);
  277.   if (view)
  278.     {
  279.       for (i = 0; i < NUM_DISPLAY_AREAS; i++)
  280.         {
  281.  
  282.           /* Get the area to be used by the drawport */
  283.           VOobBox (TdrGetNamedObject (TviGetDrawing (view), AreaName[i]),
  284.                    &box, &dummy);
  285.  
  286.           /* Since the "world coordinates" are -16K to 16K and the
  287.           |  "virtual screen coordinates" are    0K to 32K we can
  288.           |  just add 16K to the "world" to translate it to "virtual"
  289.           */
  290.           DpArea[i].ll.x = box.ll.x + XMAX;
  291.           DpArea[i].ll.y = box.ll.y + YMAX;
  292.           DpArea[i].ur.x = box.ur.x + XMAX;
  293.           DpArea[i].ur.y = box.ur.y + YMAX;
  294.         }
  295.       (VOID) TviDestroy (view);
  296.     }
  297.   else
  298. #ifdef WINNT
  299.     {
  300.     (VOID) sprintf (dsp_message,"Using the default layout.\n");
  301.     MessageBox (GetFocus (), dsp_message, "Display Manager", MB_OK);
  302.     }
  303. #else
  304.     (VOID) printf ("Using the default layout.\n");
  305. #endif
  306.   /* Load the menu view. Make it stretch into the menu area. */
  307.   view = TviLoad (menu_name);
  308.   if (!view)
  309. #ifdef WINNT
  310.     {
  311.     (VOID)sprintf(dsp_message,"You must specify a menu V_view file or\n  the default V_view must be available.\nDisplay Manager arguments will override defaults:\n  (menu name) (initial display name) (layout name)\nDefaults: \n  \"%s\" \"%s\" \"%s\"\n",MenuViewName, DisplayViewName, LayoutViewName);
  312.     MessageBox ( GetFocus (), dsp_message, "Display Manager", MB_ICONSTOP );
  313.     exit( EXIT_ERR );
  314.     }
  315. #else
  316.     {
  317.       (VOID) printf ("You must specify a menu view file or\n");
  318.       (VOID) printf ("  the default view, %s, must be available.\n",
  319.                      MenuViewName);
  320.       (VOID) printf ("Display Manager arguments will override defaults:\n");
  321.       (VOID) printf ("  (menu name) (initial display name) (layout name)\n");
  322.       (VOID) printf ("Defaults: \n");
  323.       (VOID) printf ("  \"%s\" \"%s\" \"%s\"\n",
  324.                      MenuViewName, DisplayViewName, LayoutViewName);
  325.  
  326.       exit (EXIT_ERR);
  327.     }
  328. #endif
  329.   VOobBox (TviGetDrawing (view), &wvp, &dummy);
  330.   MenuDp = TdpCreateStretch (DVscreen, view, &DpArea[MENU], &wvp);
  331.   MenuDsl = TviGetDataSourceList (view);
  332.   (VOID) TdlOpenData (MenuDsl);
  333.  
  334.   /* For "looping", create a list of views referenced by the display menu */
  335.   DemoCount = 0;
  336.   (VOID) TdrForEachNamedObject (TviGetDrawing (view),
  337.                                 (TDRFOREACHNAMEDOBJFUNPTR) GetDemoNames,
  338.                 (ADDRESS) & DemoCount);
  339.  
  340.   /* Load the initial display view.*/
  341.   view = TviLoad (display_name);
  342.   if (!view)
  343.     view = TviCreate ();
  344.   DisplayDp = TdpCreate (DVscreen, view, &DpArea[DISPLAY], (RECTANGLE *) NULL);
  345.  
  346.   /* Draw both drawports */
  347.   (VOID) TdpDraw (MenuDp);
  348.   (VOID) TdpDraw (DisplayDp);
  349. }
  350.  
  351. /*------------------------------------------------------------------------*/
  352. LOCAL void
  353. RedrawDisplay ()
  354. {
  355.   /* Redraw the display areas, this is called after a RESIZE or EXPOSE */
  356.   (VOID) TdpRedraw (MenuDp, (RECTANGLE *) NULL, YES);
  357.   if (Proto_Env)
  358.     (VOID) TprotoRedraw (Proto_Env);
  359.   else
  360.     (VOID) TdpRedraw (DisplayDp, (RECTANGLE *) NULL, YES);
  361. }
  362.  
  363. /*------------------------------------------------------------------------*/
  364. LOCAL void 
  365. HandleEvents (location, done)
  366.      OBJECT location;
  367.      DV_BOOL *done;
  368. {
  369.   DRAWPORT selected_dp;
  370.   INT proto_status;
  371.   INT key_test;
  372.  
  373.   /* Process the user input, Some systems generate a EXPOSE
  374.   |  event after a resize, some don't.  We'll keep track of
  375.   |  resize events so we won't end up drawing the display
  376.   |  twice.
  377.   */
  378.   switch (VOloType (location))
  379.     {
  380.       case V_KEYPRESS:
  381.          key_test = VOloKeySym(location);
  382.          RedrawDisplay();         
  383.          break;
  384.  
  385.     case V_EXPOSE:
  386.       RedrawDisplay ();
  387.       break;
  388.  
  389.     case V_RESIZE:
  390.       (VOID) TscReset (DVscreen);
  391.       break;
  392.  
  393.     case V_WINDOW_QUIT:
  394.       *done = YES;
  395.       break;
  396.  
  397.     default:
  398.       selected_dp = TloGetSelectedDrawport (location);
  399.  
  400.       /* If we pick inside the Menu Area... */
  401.       if (selected_dp == MenuDp)
  402.         *done = HandleMenu (location);
  403.  
  404.       /* Else if a demo is running pass the event to the Proto Handler */
  405.       else if (Proto_Env)
  406.         {
  407.           proto_status = TprotoHandleInput (Proto_Env, location);
  408.           if (proto_status == V_TPROTO_QUIT)
  409.             {
  410.               TprotoCleanup (Proto_Env);
  411.               Proto_Env = NULL;
  412.               (VOID) TdpDraw (DisplayDp);
  413.             }
  414.         }
  415.       break;
  416.     }
  417. }
  418.  
  419. /*------------------------------------------------------------------------*/
  420. LOCAL BOOLPARAM 
  421. HandleMenu (location)
  422.      OBJECT location;
  423. {
  424.   CHAR *obj_name;
  425.  
  426.   /* See if we "picked" not just moved the cursor */
  427.   if (VOloType (location) == V_KEYPRESS ||
  428.       VOloType (location) == V_BUTTONPRESS)
  429.     {
  430.  
  431.       /* Get the name of the object...
  432.       |     if "quit", return YES else return NO.
  433.       |     else if "loop", set the DemoLoop flag.
  434.       |     else get the object name and make it the next demo.
  435.       */
  436.       obj_name = TloGetSelectedObjectName (location);
  437.       if (obj_name)
  438.         {
  439.           if (strcmp (obj_name, "quit") == 0)
  440.             return YES;
  441.           else if (strcmp (obj_name, "loop") == 0)
  442.             {
  443.               DemoLooping = YES;
  444.               DemoIndex = -1;
  445.             }
  446. #ifndef WINNT
  447.           else if (strcmp (obj_name, "faster") == 0)
  448.             TimeoutInterval = 0;
  449.           else if (strcmp (obj_name, "slower") == 0)
  450.             TimeoutInterval = 25;
  451. #endif
  452.           else
  453.             {
  454.               /* Make this the new prototype */
  455.               DemoLooping = NO;
  456.               InitDemo (obj_name);
  457.             }
  458.         }
  459.     }
  460.   return NO;
  461. }
  462.  
  463. /*------------------------------------------------------------------------*/
  464. LOCAL void 
  465. InitDemo (view_name)
  466.      CHAR *view_name;
  467. {
  468.   DRAWPORT_ATTRIBUTES dp_atts;
  469.  
  470.   /* Setup the display attributes */
  471.   dp_atts.vvp = &DpArea[DISPLAY];
  472.   dp_atts.wvp = NULL;
  473.   dp_atts.stretch_flag = (DV_BOOL) NO;
  474.  
  475.   /* Clean up the old display, before drawing the new one */
  476.   if (Proto_Env)
  477.     TprotoCleanup (Proto_Env);
  478.  
  479.   /* Set Proto_Env to NULL, so if a timeoutproc gets called during
  480.   |  TprotoInt... we don't have an invalid proto_env.
  481.   */
  482.   Proto_Env = 0;
  483.  
  484.   /* Initialize the new demo display */
  485.   Proto_Env = TprotoInit (DVscreen, view_name, &dp_atts);
  486.  
  487.   /* If the display wasn't loaded, display the default view */
  488.   if (!Proto_Env)
  489.     (VOID) TdpDraw (DisplayDp);
  490. }
  491.  
  492. /*------------------------------------------------------------------------*/
  493. /* ARGSUSED */
  494. LOCAL ADDRESS 
  495. GetDemoNames (obj, name, num_displays)
  496.      OBJECT obj;
  497.      CHAR *name;
  498.      INT *num_displays;
  499. {
  500.  
  501.  
  502.   /* Don't include the "loop" and "quit" named objects */
  503.   if (S_STRCMP ("ArrowPath", name) == 0 ||
  504.       S_STRCMP ("loop", name) == 0 ||
  505.       S_STRCMP ("quit", name) == 0)
  506.     return (ADDRESS) NULL;
  507.  
  508.   /* Save this display name */
  509.   DemoName[*num_displays] = VIstrclone (name);
  510.   *num_displays = *num_displays + 1;
  511.  
  512.   /* Don't go beyond the max number of displays */
  513.   if (*num_displays == MAX_DEMOS)
  514.     return (ADDRESS) - 1;
  515.   else
  516.     return (ADDRESS) NULL;
  517. }
  518.  
  519. /*------------------------------------------------------------------------*/
  520. LOCAL void 
  521. SwitchDemo ()
  522. {
  523.   /* Get the next demo to display */
  524.   DemoIndex++;
  525.   DemoIndex = (DemoIndex >= DemoCount) ? 0 : DemoIndex;
  526.  
  527.   /* Display the new demo */
  528.   InitDemo (DemoName[DemoIndex]);
  529.  
  530.   /* Reset the loop timer */
  531.   InitTimer ();
  532. }
  533.  
  534. /*------------------------------------------------------------------------*/
  535. LOCAL void 
  536. DisplayCleanup ()
  537. {
  538.   VIEW view;
  539.  
  540.   /* Clean up the menu area */
  541.   view = TdpGetView (MenuDp);
  542.   (VOID) TdpDestroy (MenuDp);
  543.   (VOID) TviDestroy (view);
  544.  
  545.   /* Clean up the default display area */
  546.   view = TdpGetView (DisplayDp);
  547.   (VOID) TdpDestroy (DisplayDp);
  548.   (VOID) TviDestroy (view);
  549.  
  550.   /* Clean up the current demo */
  551.   if (Proto_Env)
  552.     TprotoCleanup (Proto_Env);
  553.  
  554.   /* Close the screen */
  555.   (VOID) TscClose (DVscreen);
  556. }
  557.